﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HelloMinasan.Domain;
using HelloMinasan.Caching;
using HelloMinasan.Business;
using System.Web.Mvc;
using HelloMinasan.Caching.CacheObjects;
using HelloMinasan.Resources;
using HelloMinasan.Framework.Web;

namespace HelloMinasan.Web.ViewModels
{
    public class UserBasicInfoModel
    {
        public static UserBasicInfoModel Create(int userId)
        {
            IUserService userService = DependencyResolver.Current.GetService<IUserService>();
            UserCache userCache = CacheService.Get<UserCache>(
                    CacheKeys.UserBasic(userId), CacheDuration.Long,
                    () => UserCache.Create(userService.Load(userId)));

            UserBasicInfoModel userBasicInfoModel = new UserBasicInfoModel();
            userBasicInfoModel.Id = userCache.Id;
            userBasicInfoModel.DisplayName = userCache.DisplayName;
            userBasicInfoModel.FullName = userCache.FullName;
            userBasicInfoModel.Gender = userCache.Gender;
            userBasicInfoModel.CreatedTime = userCache.CreatedTime;
            userBasicInfoModel.UserType = userCache.UserType;
            userBasicInfoModel.HasAvatar = userCache.HasAvatar;
            userBasicInfoModel.Location = userCache.Location;

            userBasicInfoModel.MainSpecialityName = userCache.MainSpeciatylName;

            return userBasicInfoModel;
        }

        public int Id { get; set; }
        public string Gender { get; set; }

        public string DisplayName { get; set; }
        public string FullName { get; set; }

        public DateTime CreatedTime { get; set; }

        public int NumberOfFriendRequests { get; set; }

        public UserType UserType { get; set; }

        public bool HasAvatar { get; set; }
        public string Location { get; set; }

        public string MainSpecialityName { get; set; }

        public string NameOnWeb
        {
            get
            {
                if (UserType == Domain.UserType.Admin/* || UserType == Domain.UserType.Partner*/)
                {
                    //if (UserType == Domain.UserType.Admin)
                        return Resource.Common_AdministratorSystem;
                    //return UserType == Domain.UserType.Partner ? string.Format(Resource.Common_Dr + " {0}", FullName) : FullName;
                    //return FullName;
                }

                //if (HttpContext.Current.User.Identity.Name == MyPartnerId.ToString())
                //{
                //    return FullName;
                //}

                return DisplayName;
            }
        }
    }


    public class UserBasicInfoModelEqualityComparer : IEqualityComparer<UserBasicInfoModel>
    {

        public bool Equals(UserBasicInfoModel x, UserBasicInfoModel y)
        {
            if (Object.ReferenceEquals(x, y))
            {
                return true;
            }
            if (x == null || y == null)
            {
                return false;
            }
            return x.Id == y.Id;
        }

        public int GetHashCode(UserBasicInfoModel obj)
        {
            return obj.Id;
        }
    }
}